<!-- 编辑功能模板 -->
<template>
  <div style="width: 100%; height: 100%; overflow: hidden;">
    <div class="title">
      <div class="name">{{menuName}}</div>
      <div class="butClose" @click="closeWin"></div>
    </div>
    <div class="editBody">
      <div class="groupLine">
        <div class="line"></div>
        <div class="gName">基础信息</div>
        <div class="line"></div>        
      </div>
      <div style="clear: both; width: 100%;">
        <div v-for="item in paramData.params" :style="item.type == 'textarea' ? 'width: 100%; clear: both;' : 'width: 50%; float: left;'">
          <field-operate :theData="item" :theOrg="orgId" />
        </div>
      </div>
    </div>
    <div class="button">
      <button class="butTwo" @click="closeWin">取 消</button>
      <button class="butOne" @click="submitData">保 存</button>
    </div>
    <div class="floatBG2" v-show="allowOpen"></div>
    <div class="selectData" v-show="allowOpen == true && openType == 'selectData'">
      <select-data :theData="openData" :theOrg="orgId"></select-data>
    </div>
    <div class="typeData" v-show="allowOpen == true && openType == 'typeData'">
      <type-data :theData="openData" :theOrg="orgId"></type-data>
    </div>
  </div>
</template>
<script>
import selectData from "./selectData.vue";
import typeData from "./typeData.vue";
import fieldOperate from "./fieldOperate.vue";
export default {
  data() {
    return {
      menuName: "",
      menuKey: "",
      dataId:0,
      orgId:0,
      limitState:0,
      limitClew:'',
      openData:{},
      paramData:{},
      tableData:{},
      openType:'',
      showUrl:'',
      submitUrl:'',
      allowOpen: false
    };
  },
  components:{
    selectData,
    typeData,
    fieldOperate
  },
  props:{
    menuData:{
      type:Object,
      required:true
    },
    theOrg:{
      type:Number,
      required:true
    }
  },
  created() {
    
  },
  watch: {
    menuData(data){
      this.menuKey = this.menuData.key;
      this.menuName = this.menuData.name;
      this.dataId = this.menuData.dataId;
      this.orgId = this.theOrg;
      this.paramData = {};
      this.tableData = {};
      this.submitUrl = '';
      this.showUrl = '';
      if(this.menuData.openType == 'baseEdit')
        this.showData();
    }
  },
  methods: {
    //显示数据与组件
    async showData(){
      if(this.menuData != null && this.menuData.showUrl != null){
        let toUrl = this.changeUrl(this.menuData.showUrl);
        let { data: res } = await this.$http.get(toUrl);
        if (res.result) {
          this.limitState = res.obj.showState;
          this.limitClew = '';
          if(this.limitState != null && this.limitState > 0){
            if(this.limitState == 1)
              this.limitClew = '已审核';
            else if(this.limitState == 2)
              this.limitClew = '已禁用';
          }
          this.submitUrl = res.obj.submitUrl;
          this.paramData = res.obj.formParam;
          this.menuName = res.obj.menuName;
          for(let item of this.paramData.params.filter(x => x.allow)){
            if(item.type.toLowerCase().includes('select') || item.type == 'radio'){
              let theVal = item.selectValue ? item.selectValue.toString() : '0';
              this.tableData[item.actionValue] = theVal.toString();
            } else{
              this.tableData[item.paramName] = item.value;
            }            
          }
        }else{
          this.clewMsg(res.type,res.msg);
        }
      }
    },
    //提交保存数据
    async submitData(){
      if(this.limitState <= 0){
        var verifyItems = this.paramData.params.filter(x => x.allow && x.null);
        var verify = true;
        var message = '请注意：<br/><br/>';
        //验证必填字段是否有填写
        for(let item of verifyItems){          
          if(item.type != 'radio'){
            let theVal = item.type.toLowerCase().includes("select") ? 
              this.tableData[item.actionValue] : this.tableData[item.paramName];
            if(item.type == 'int' || item.type == 'float' || item.type.toLowerCase().includes("select")){            
              if(theVal == null || theVal <= 0){
                message += '字段#'+item.name+'#，为必填项，请确认;<br/>';
                verify = false;
              }
            }else{
              if(theVal == null || theVal.trim().length <= 0) {
                message += '字段#'+item.name+'#，为必填项，请确认;<br/>';
                verify = false;
              }
            }
          }          
        }
        if(verify){
          //post 数据
          this.tableData['id'] = this.dataId;
          var postUrl = this.submitUrl;
          var dataParam = '{ '+this.menuKey+'Data: '+JSON.stringify(this.tableData)+' }';
          var postData = {
            dataId: this.dataId,
            orgId: this.theOrg,
            menuKey: this.menuKey,
            dataParam: dataParam
          };
          let { data: res } = await this.$http.post(postUrl, postData);
          if (res.result) {
            this.clewMsg(1,'操作成功');
            console.log('submit data new ID: '+res.dataId);
          } else {
            this.clewMsg(res.type,res.msg);
          }
        }else{
          this.clewMsg(3,message);
        }
      }else{
        this.clewMsg(2,'很抱歉，该单据处于#'+this.limitClew+'#，不能操作');
      }
    },
    //字段组件修改值
    updateChangeVal(data){
      console.log('update data:>>');
      console.log(data);
      this.tableData[data.param] = data.value.toString();
    },
    //打开编辑窗口
    openWin(data) {
      this.openData = data;
      this.openType = data.type;
      this.allowOpen = true;
    },
    //关闭子窗口
    closeSubWin(){
      this.allowOpen = false;
      this.openType = '';
      this.openData = {};
    },
    //关闭本窗体
    closeWin(){
      this.$parent.closeEdit();
    },    
    //处理URL自增加组织ID
    changeUrl(url){
      if(/\?|&/.test(url)){
        url += '&orgId=' + this.theOrg;
      }else{
        url += '?orgId=' + this.theOrg;
      }      
      console.log("changeURL:" + url);
      return url;
    },
    //提款信息
    clewMsg(type,mes){
      var theType = 'warning';
      if(type == 1)
        theType = 'success';
      else if(type == 3)
        theType = 'error';
      this.$message({
        dangerouslyUseHTMLString:true,
        message: mes,
        type: theType
      });
    }
  }
};
</script>
<style lang="less" scoped>
</style> 
<style>
  .title{
    clear: both;
    width: calc(100% - 50px);
    height: 40px;
    padding: 8px 25px;
    border-bottom: solid 1px #DDD;
  }  
  .name{
    float: left;
    height: 40px;
    line-height: 40px;
    color: #555;
    font-size: 16px;
    text-align: left;
  }
  .butClose{
    float: right;
    height: 40px;
    width: 40px;
    cursor: pointer;
    background: url(../assets/img/close.png) no-repeat;
    background-size: 50% 50%;
    background-position: center right;
  }  
  .editBody{
    clear: both;
    padding: 20px 20px 40px 20px;
    width: calc(100% - 40px);
    max-height: 420px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .button{
    clear: both;
    width: calc(100% - 20px);
    height: 40px;
    padding: 10px;
    border-top: solid 1px #DDD;
    align-items: right;
    text-align: right;
  }
  .groupLine{
    display: flex;
    width: 100%;
    margin: 0 auto;
    padding-bottom: 10px;
    .line{
        flex: 1;
        position: relative;
        top: -12px;
        border-bottom: 1px solid #EEE;
    }
    .gName{
        padding: 0 14px;
        font-size: 16px;
        color:#666;
    }
  }
  .selectData{
    position: fixed;
    z-index: 777;
    top: 100px;
    left: calc((100% - 1500px) / 2);
    width: 1500px;
    max-height: 650px;
    overflow: hidden;
    background: #FFF;
    border-radius: 5px;
    box-shadow: 0 1px 2px 1px rgba(0,0,0,0.15);
  }
  .typeData{
    position: fixed;
    z-index: 888;
    top: 100px;
    left: calc((100% - 800px) / 2);
    width: 800px;
    max-height: 650px;
    overflow: hidden;
    background: #FFF;
    border-radius: 5px;
    box-shadow: 0 1px 2px 1px rgba(0,0,0,0.15);
  }
  .floatBG2{
    position: fixed;
    z-index: 556;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.3);
  }
</style>
